<?php namespace shared\model\project;
/**
 * @subpackage  \shared\model\project
 *
 * @copyright   Copyright (C) 2013 - 2014 Mailerm, Inc. All rights reserved.
 */

 # Load Libraries
require_once MPATH_LIBRARIES . '/database/MySQL.php';

use \MySQL as MySQL;

# Load Configuration
require_once MPATH_MODULES . '/configuration.php';

// Save project into database
// @return bool
function save($name, $status, $process, $start_time, $end_time, $create_time, $update_time, $desc) {
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);   
    # Insert All
    $database->prepare('INSERT INTO `' . TBL_PROJECT . '` (`id`, `name`, `status`, `process`, `start_time`, `end_time`, `create_time`, `update_time`, `desc`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
    $database->execute(null, $name, $status, $process, $start_time, $end_time, $create_time, $update_time, $desc);
    return $database->insert_id() < 0 ? false : true;
}

// Update project
// @return bool
function update($name, $desc, $id) {
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);   
    $database->prepare('UPDATE `' . TBL_PROJECT . '` SET `name`=?, `desc`=?, `update_time`=? WHERE `id`=?');
    $database->execute($name, $desc, date(FORMAT_DATE), $id);
    return $database->affected_rows() < 0 ? false : true;
}

// Get project by id
// @return array
function get($id) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('SELECT `id`,`name`,`status`,`process`,`start_time`,`end_time`,`create_time`,`update_time`,`desc` FROM `' . TBL_PROJECT . '` WHERE `id`=? AND `status`!=? LIMIT 1');
    $database->execute($id, PROJECT_STATUS_DELETED);
    $results = $database->results();
    # Num Rows
    if($database->num_rows() < 1){
        return null;
    }
    return $results[0];
}

// Start project by id
// @return affected
function start($id) {    
    $now = date(FORMAT_DATE);
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_PROJECT . '` SET `process`=?, `start_time`=?, `update_time`=? WHERE `id`=? AND `status`=? AND `process`=?');
    $database->execute(PROJECT_PROCESS_ONGOING, $now, $now, $id, PROJECT_STATUS_NORMAL, PROJECT_PROCESS_PREPARE);
    return $database->affected_rows();
}

// Stop project by id
// @return affected
function stop($id) {
    $now = date(FORMAT_DATE);
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_PROJECT . '` SET `process`=?, `end_time`=?, `update_time`=? WHERE `id`=? AND `status`=? AND `process`=?');
    $database->execute(PROJECT_PROCESS_FINISHED, $now, $now, $id, PROJECT_STATUS_NORMAL, PROJECT_PROCESS_ONGOING);
    return $database->affected_rows();
}

// Pause project by id
// @return affected
function pause($id) {
    $now = date(FORMAT_DATE);
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_PROJECT . '` SET `status`=?, `update_time`=? WHERE `id`=? AND `status`=? AND (`process`=? OR `process`=?)');
    $database->execute(PROJECT_STATUS_PAUSED, $now, $id, PROJECT_STATUS_NORMAL, PROJECT_PROCESS_ONGOING, PROJECT_PROCESS_PREPARE);
    return $database->affected_rows();
}

// Restore project by id
function restore($id) {
    $now = date(FORMAT_DATE);
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_PROJECT . '` SET `status`=?, `update_time`=? WHERE `id`=? AND `status`=?');
    $database->execute(PROJECT_STATUS_NORMAL, $now, $id, PROJECT_STATUS_PAUSED);
    return $database->affected_rows();
}

// Count project
// @return count
function count() {
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    # Count All
    $database->prepare('SELECT COUNT(`id`) AS \'count\' FROM `' . TBL_PROJECT . '` WHERE `status`!=?');
    $database->execute(PROJECT_STATUS_DELETED);
    $results = $database->results();
    # Num Rows
    if($database->num_rows() < 1) {
        return -1;
    }
    return intval($results[0]['count']);
}

// Find available projects
// @return array
function find($start, $size) {
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('SELECT `id`,`name`,`status`,`process`,`create_time`,`update_time`,`start_time`,`end_time`,`desc` FROM `' . TBL_PROJECT . '` WHERE `status`!=? ORDER BY `id` DESC LIMIT ?,?');
    $database->execute(PROJECT_STATUS_DELETED, $start, $size);
    $users = $database->results();
    return $users;
}

// Delete projects
// @return affected
function delete($ids) {
    $tmp = '0';
    foreach ($ids as $key => $value) {
        $tmp = $tmp . ',' . $value;
    }
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_PROJECT . '` SET `status`=? WHERE `process`!=? AND `id` IN (' . $tmp . ')');
    $database->execute(PROJECT_STATUS_DELETED, PROJECT_PROCESS_ONGOING);
    return $database->affected_rows();
}